home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 080 (1988-11-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 080 (1988-11-15)(Ossowski, Stefan)(DE)(PD).adf / CLI_Utilities / Filter / Filter.c < prev    next >
C/C++ Source or Header  |  1988-08-14  |  3KB  |  91 lines

  1. /*  Filter (C) Copyright 1987 by Russell Wallace. This program is in
  2.     the public domain and may be freely distributed. It may not be used
  3.     commercially without the author's permission. If you find this program
  4.     useful,please send a small donation to the author at:
  5.         24 Lower Georges St.
  6.         Dunlaoghaire
  7.         Co. Dublin
  8.         Ireland
  9.     to help finance further software development. Contributions in kind i.e.
  10.     Amiga disks welcome. If you wish to discuss any aspect of the Amiga or
  11.     computers in general,write or phone 807094.
  12.  
  13.     Usage: Filter [>outputfile] inputfile
  14.     Filter takes the input file and copies it to an output file using only
  15.     valid ASCII characters from the input file. Useful for recovering
  16.     corrupted files, extracting text from object files or converting IFF to
  17.     ASCII.
  18.  
  19.     If you give copies of this program to anyone,please distribute source
  20.     code with object code to allow examination of programming techniques;
  21.     the Amiga programming environment is so complex that source code is
  22.     very valuable as an information source */
  23.  
  24. #include <libraries/dos.h>
  25. #include <exec/types.h>
  26.  
  27. #define MAXBUFFER 256L        /* length of I/O buffers */
  28. #define EOF 257
  29.  
  30. unsigned char inbuffer[MAXBUFFER],outbuffer[MAXBUFFER];
  31. struct FileInfoBlock *fp;
  32. struct FileInfoBlock *stdout;
  33.  
  34. unsigned short charin ()            /* input a character */
  35. {
  36.     static short inpoint=MAXBUFFER-1;
  37.     static short maxin  =MAXBUFFER-1;
  38.     LONG Read ();
  39.     if (++inpoint>=maxin)
  40.     {
  41.         maxin=Read (fp,inbuffer,MAXBUFFER);
  42.         inpoint=0;
  43.     }
  44.     if (maxin==0)
  45.         return (EOF);
  46.     return (unsigned short)(inbuffer[inpoint]);
  47. }
  48.  
  49. charout (ch)                /* output a character */
  50. unsigned short ch;
  51. {
  52.     static long outpoint=-1;
  53.     LONG Write ();
  54.     if ((++outpoint>=MAXBUFFER)||(ch==EOF))
  55.     {
  56.         Write (stdout,outbuffer,outpoint);
  57.         outpoint=0;
  58.     }
  59.     outbuffer[outpoint]=(unsigned char)ch;
  60. }
  61.  
  62. main (argc,argv)
  63. int argc;        /* number of arguments input by user */
  64. char *argv[];    /* pointers to each argument */
  65. {
  66.     unsigned short ch;
  67.     struct FileInfoBlock *Open ();
  68.     struct FileInfoBlock *Output ();
  69.     stdout=Output ();
  70.     if ((argc<2)||(*argv[1]=='?'))    /* If no arguments or ?,give help */
  71.     {
  72.         Write (stdout,"Usage: Filter [>outputfile] inputfile\n",38L);
  73.         exit (10);
  74.     }
  75.     if ((fp=Open (argv[1],MODE_OLDFILE))==0)    /* Can't open file */
  76.     {
  77.         Write (stdout,"Couldn't open file for filter\n",30L);
  78.         exit (10);
  79.     }
  80.     for (;;)
  81.     {
  82.         ch=charin ();
  83.         if (ch==EOF)
  84.             break;
  85.         if ((ch>31 && ch<127) || ch==10 || ch==9)
  86.             charout (ch);
  87.     }
  88.     charout (EOF);        /* Write out buffered output */
  89.     Close (fp);
  90. }
  91.